package com.controller;

import com.api.CommonResult;
import com.entity.Onecharacter;
import com.entity.table.HzstorytellingTableDef;
import com.entity.table.OnecharacterTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.entity.Hzstorytelling;
import com.service.HzstorytellingService;

import java.io.Serializable;
import java.util.List;

/**
 * 首页-故事介绍 控制层。
 *
 * @author admin
 * @since 2024-06-19
 */
@RestController
@RequestMapping("/hzstorytelling")
public class HzstorytellingController {

    @Autowired
    private HzstorytellingService hzstorytellingService;

    /**
     * 添加首页-故事介绍。
     *
     * @param hzstorytelling 首页-故事介绍
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    public boolean save(@RequestBody Hzstorytelling hzstorytelling) {
        return hzstorytellingService.save(hzstorytelling);
    }


    /**
     * 根据主键删除首页-故事介绍。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return hzstorytellingService.removeById(id);
    }

    /**
     * 根据主键更新首页-故事介绍。
     *
     * @param hzstorytelling 首页-故事介绍
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
//    @PutMapping("update")
//    public boolean update(@RequestBody Hzstorytelling hzstorytelling) {
//        return hzstorytellingService.updateById(hzstorytelling);
//    }

    /**
     * 查询所有首页-故事介绍。
     *
     * @return 所有数据
     */
//    @GetMapping("list")
//    public List<Hzstorytelling> list() {
//        return hzstorytellingService.list();
//    }

    /**
     * 根据首页-故事介绍主键获取详细信息。
     *
     * @param id 首页-故事介绍主键
     * @return 首页-故事介绍详情
     */
    @GetMapping("getInfo/{id}")
    public Hzstorytelling getInfo(@PathVariable Serializable id) {
        return hzstorytellingService.getById(id);
    }

    /**
     * 分页查询首页-故事介绍。
     *
     * @param hzstorytelling 分页对象
     * @return 分页对象
     */
    @PostMapping("update")
    @ResponseBody
    public CommonResult update(Hzstorytelling hzstorytelling) {
        try {
            boolean r = hzstorytellingService.updateById(hzstorytelling);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = hzstorytelling.getStorytelling() + "已存在";
            return CommonResult.failed(msg);
        }
    }

    @GetMapping("page")
    public Page<Hzstorytelling> page(Page<Hzstorytelling> page) {
        return hzstorytellingService.page(page);
    }

    @GetMapping(value = "/list")
    public String typeList(ModelMap model, @RequestParam(value = "storytelling", defaultValue = "") String storytelling,
                           @RequestParam(value = "p", defaultValue = "1") int p,
                           @RequestParam(value = "pageize", defaultValue = "10") int pageSize) {
        QueryWrapper queryWrapper = QueryWrapper.create().where(HzstorytellingTableDef.HZSTORYTELLING.STORYTELLING.like(storytelling));
        Page<Hzstorytelling> page = new Page<>();
        page.setPageSize(pageSize);
        page.setPageNumber(p);
        Page<Hzstorytelling> pageInfo = hzstorytellingService.page(page,queryWrapper);
        model.addAttribute("page", pageInfo);
        model.addAttribute("total", pageInfo.getTotalRow());
        return "back/onecharacter-list";

    }
    @GetMapping(value = "updateView")
    public String updateView(int id, ModelMap model) {
        Hzstorytelling hzstorytelling = hzstorytellingService.getById(id);
        model.addAttribute("onecharacter", hzstorytelling);
        return "back/onecharacter-edit";
    }

    @PostMapping("del")
    @ResponseBody
    public CommonResult del(Integer id) {
        try {
            boolean r = hzstorytellingService.removeById(id);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
    }
    @GetMapping(value = "/addView")
    public String addTypeView() {
        return "back/onecharacter-add";
    }
}
